# Python 2.6.4
# Project Euler, Problem 93
# Copyright 2010 Talha Zaman

def perms(s, l, r=True):
    if l<=1: return [d for d in s]
    p = []
    for i in range(len(s)):
        subs = perms(s[:i] + s[i+1:], l-1, r) if r else perms(s, l-1, r)
        for j in subs: p.append(s[i] + j)
    return p

def rpn(s):
    stack = []
    for i in s:
        if i.isdigit(): stack.append(int(i))
        elif len(stack)>1:
            b, a = stack.pop(), stack.pop()
            if i=="+": stack.append(a+b)
            elif i=="-": stack.append(a-b)
            elif i=="*": stack.append(a*b)
            elif i=="/" and b: stack.append(a/float(b))
            else: return 0
    return int(stack[0]) if len(stack)==1 and int(stack[0])==stack[0] else 0

def seqlen(s):
    c = set()
    for d in perms(s, 4):
        for op in perms("+-*/", 3, False):
            c.add(rpn(d[:2] + op[0] + d[2] + op[1] + d[3] + op[2]))
            c.add(rpn(d[:2] + op[0] + d[2:] + op[1:]))
            c.add(rpn(d[:3] + op[0] + d[3] + op[1:]))
            c.add(rpn(d[:3] + op[:2] + d[3] + op[2]))
            c.add(rpn(d+op))
    i = 1
    while i in sorted(c): i+=1
    return i-1

msl, mabcd = 0, 0
for a in range(1,7):
    for b in range(a+1, 8):
        for c in range(b+1, 9):
            for d in range(c+1, 10):
                abcd = str(a) + str(b) + str(c) + str(d)
                sl = seqlen(abcd)
                if msl < sl: msl, mabcd = sl, abcd
print msl, mabcd #51, 1258
